<?php

namespace App\Service;

use App\Models\EwLiveRecord;

class LiveService extends BaseService
{
	const LIVE_TRUE = 1;		//直播标识：是
	const LIVE_FALSE = 0;		//直播标识：否 回播
	const DEL_TRUE = 1;			//删除标识：是
	const DEL_FALSE = 0;		//删除标识：否

    public static function getList($params){
		
		return EwLiveRecord::query()
		        ->columns(['id','title','thumb','avatar','nickname','member_id','room_id','created_time','watch_count','is_live','vod','is_del'])
		        ->where("is_del = :is_del:")
				->bind(['is_del' => 0])
				->limit($params->page_num,($params->p-1)*$params->page_num)
				->execute();
	}
	
    public static function getPlayBackList($params){
		
		return EwLiveRecord::query()
				->columns(['id','title','thumb','avatar','nickname','room_id','created_time','watch_count'])
				->where("is_live = :is_live: AND member_id = :member_id: AND is_del = :is_del:")
				->bind(['is_live' => 0, 'is_del' => 0,'member_id'=>$params->host_id])
				->limit($params->page_num,($params->p-1)*$params->page_num)
				->execute();
	}	
	
	public static function getInfo($id = 0){
		return EwLiveRecord::findFirst($id);
	}
	
	/**
	 * [getReplay 回拨详情]
	 * @param  integer $id [description]
	 * @return [type]      [description]
	 */
	public static function getReplay($id = 0)
	{
		return EwLiveRecord::findFirst([
			"is_live = :is_live: AND id = :id:",
			"bind" => [
				"is_live" => 0,
				"id" => $id,
			],
		]);
	}

	//通过用户id取直播记录
	public static function getMemberIdInfo($member_id = 0){
		return EwLiveRecord::findFirst([
			"is_live = :is_live: AND member_id = :member_id: AND is_del = :is_del:",
			"bind" => [
				"is_live" => 1,
				"is_del"  => 0,
				"member_id" => $member_id,
			],
		]);
	}
	
	//关闭直播
	public static function close($raw){
		
		if($raw->host_id){
			$data = static::getMemberIdInfo($raw->host_id);
		}else{ 
			$data = static::getMemberIdInfo($raw->member_id);
			if($data){
				$data->time_span = time() - strtotime($data->created_time);
				$data->is_live = 0;
				$data->save();
			}
		}
		
		return $data;
	}	
	
	
	public static function countPlay($member_id){
		return EwLiveRecord::count([
			"is_live = :is_live: AND member_id = :member_id: AND is_del = :is_del:",
			"bind" => [
				"is_live" => 0,
				"is_del"  => 0,
				"member_id" => $member_id,
			],
		]);
	}
	
	public static function count(){
		return EwLiveRecord::count();
	}

	public static function save($data){
		$row = static::getMemberIdInfo($data->member_id);
		if(isset($row->id) && $row->id){
			static::echoJson('Liveing',402);
		}
		$model = new EwLiveRecord();
		return $model->save((array)$data);
	
	}

	/**
	 * [delete 删除数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function delete($id)
	{
		if (isset($id) && $id)
			$data = EwLiveRecord::findFirst($id);
		
		if ($data){
			$data->is_del = 1;
			return $data->save();
		}

		return NULL;
	}

	/**
	 * [restore 恢复数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function restore($id)
	{
		if (isset($id) && $id)
			$data = EwLiveRecord::findFirst($id);
		
		if ($data){
			$data->is_del = 0;
			return $data->save();
		}

		return NULL;
	}

	/**
	 * [delete 删除数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function forever($id)
	{
		if (isset($id) && $id)
			$row = EwLiveRecord::findFirst($id);

		if ($row)
			return $row->delete();

		return NULL;
	}	

}
